class UsersController < ApplicationController
  skip_before_filter :redirect_if_not_active, :only => ["edit", "update"]
  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    
    render :layout => "two_columns_tiny_left"
  end

  def new
    @user = User.new
  end

  def edit
    @user = current_user
    
    if params[:id] and params[:id].to_i != @user.id
      redirect_to edit_user_url(current_user) and return
    end
    
    render :layout => "one_column"
  end
  
  def preferences
    @user = current_user
    
    if params[:id] and params[:id].to_i != @user.id
      redirect_to edit_user_url(current_user) and return
    end
  end

  def create
    @user = User.new(params[:user])
    
    if @user.save
      redirect_to @user
    else
      render :action => "new"
    end
  end
  
  def update
    @user = User.find(params[:id])
    
    unless @user.active?
      @user.active = 1
    end
    
    if @user.update_attributes(params[:user])
      if @user.active_was == 0
        UserMailer.deliver_welcome_mail @user
        redirect_to root_path and return
      else
        redirect_to @user and return
      end
    else
      render :action => "edit"
    end
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml  { head :ok }
    end
  end
end
